package com.weilan.ark3.sqlite;

import com.weilan.ark3.entity.ParamDto;

import java.sql.*;

public class SQLiteParamUtils {

    public static ParamDto query() {
        ParamDto paramDto = new ParamDto();
        String sql = "SELECT * FROM param";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            PreparedStatement statement = conn.prepareStatement(sql);
            ResultSet rs = statement.executeQuery();
            paramDto.setId(rs.getInt("id"));
            paramDto.setDeviceNum(rs.getInt("deviceNum"));
            paramDto.setP_max(rs.getString("p_max"));
            paramDto.setP_min(rs.getString("p_min"));
            paramDto.setSoc_h(rs.getString("soc_h"));
            paramDto.setSoc_l(rs.getString("soc_l"));
            paramDto.setBack_power(rs.getString("back_power"));
            paramDto.setGrid_alw(rs.getInt("grid_alw"));
            paramDto.setOffgrid_alw(rs.getInt("offgrid_alw"));
            paramDto.setStep_size(rs.getInt("step_size"));
            paramDto.setPattern_flag(rs.getInt("pattern_flag"));
            paramDto.setAnti_reflux(rs.getInt("anti_reflux"));
            paramDto.setPv_max(rs.getInt("pv_max"));
            paramDto.setExpansion_mark(rs.getInt("expansion_mark"));
            paramDto.setBcm_socl(rs.getInt("bcm_socl"));
            paramDto.setPv_mode(rs.getInt("pv_mode"));
            //statement.executeQuery();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return paramDto;
    }


    //修改数据
    //public static void update(Integer deviceNum,String p_max,String p_min,String soc_h,String soc_l){
    public static void update(ParamDto paramDto){
        int deviceNum = paramDto.getDeviceNum();
        String back_power = paramDto.getBack_power();
        String soc_l = paramDto.getSoc_l();
        String soc_h = paramDto.getSoc_h();
        String p_max = paramDto.getP_max();
        String p_min = paramDto.getP_min();
        String sql = "UPDATE param SET deviceNum = ?, p_max = ?, p_min = ? , soc_h = ? , soc_l = ? ,back_power = ? WHERE id = 1";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setInt(1,deviceNum);
            statement.setString(2,p_max);
            statement.setString(3,p_min);
            statement.setString(4,soc_h);
            statement.setString(5,soc_l);
            statement.setString(6,back_power);
            int i = statement.executeUpdate();
            if (i > 0){
                System.out.println("更新成功！");
            }else {
                System.out.println("更新失败！");
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void updateToSOCL(int  value){
        String sql = "UPDATE param SET soc_l = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void updateToSOCH(int  value){
        String sql = "UPDATE param SET soc_h = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void updateToBackPower(int value){
        String sql = "UPDATE param SET back_power = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void updateToAllowance(int value){
        String sql = "UPDATE param SET grid_alw = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void updateToOffAllowance(int value){
        String sql = "UPDATE param SET offgrid_alw = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void updateToStep(int value){
        String sql = "UPDATE param SET step_size = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void updateToMode(int value) {
        String sql = "UPDATE param SET pattern_flag = '%s'";
        Connection conn = null;
        try {
            conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //放逆流使能
    public static void updateToAntiReflux(int value) {
        String sql = "UPDATE param SET anti_reflux = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //更新场站最大功率
    public static void updateToMaxp(int value) {
        String sql = "UPDATE param SET p_max = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void updateToPvMax(int value){
        String sql = "UPDATE param SET pv_max = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void updateToExpansionMark(int value){
        String sql = "UPDATE param SET expansion_mark = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void updateToBcm_SOCL(int value){
        String sql = "UPDATE param SET bcm_socl = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void updateToPv_Mode(int value){
        String sql = "UPDATE param SET pv_mode = '%s'";
        try {
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/db/ARK3/fenggu.db");
            String format = String.format(sql, value);
            PreparedStatement statement = conn.prepareStatement(format);
            statement.executeUpdate();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

